/**
 * 
 */
package com.google.code.sip.ucenter.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.ucenter.dao.mongo.ApplicationDaoImpl;
import com.google.code.sip.ucenter.dao.mongo.UserCustomProfileDaoImpl;
import com.google.code.sip.ucenter.keygenerator.KeyGenerator;
import com.google.code.sip.ucenter.model.Application;
import com.google.code.sip.ucenter.service.ApplicationManager;

/**
 * @author Liuye
 * 
 */
@Service("applicationManager")
public class ApplicationManagerImpl implements ApplicationManager {
	@Autowired
	private ApplicationDaoImpl applicationDao;
	@Autowired
	private UserCustomProfileDaoImpl userCustomProfileDao;
	@Autowired
	private KeyGenerator keyGenerator;

	@Override
	public void deleteApplication(String id) {
		applicationDao.delete(id);

	}

	@Override
	public void deleteApplication(Application application) {
		applicationDao.delete(application);
		// 删除关联对象
		userCustomProfileDao.delete("application", application);
	}

	@Override
	public void findApplications(Page<Application> page) {
		applicationDao.findByPage(page);
	}

	@Override
	public Application getApplication(String id) {
		return applicationDao.get(id);
	}

	@Override
	public Application getApplicationByName(String name) {
		return applicationDao.getByName(name);
	}

	@Override
	public void saveApplication(Application application) {
		if (application.isNew()) {
			application.setAuthToken(keyGenerator.getKey());
		}
		applicationDao.save(application);

	}

	public void setKeyGenerator(KeyGenerator keyGenerator) {
		this.keyGenerator = keyGenerator;
	}

	@Override
	public long countApplication() {
		return applicationDao.count();
	}

	@Override
	public List<Application> getAllApplications() {
		return applicationDao.findAll();
	}
}
